﻿using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using My.Core.Model.Models;
namespace My.Core.Model
{
    /// <summary>
    /// 初始化数据
    /// </summary>
    public static class SeedData
    {
        /// <summary>
        /// 数据预热
        /// </summary>
        /// <param name="serviceProvider"></param>
        public static void Initialize(IServiceProvider serviceProvider)
        {
            //在某些情况下，您可能需要在方法中解析其他服务。在这种情况下，
            //请确保在使用后释放服务。确保这一点的最佳方法是创建scoped服务，例如：

            using (var serviceScope = serviceProvider.CreateScope())
            {

                var dbContext = serviceScope.ServiceProvider.GetService<MyDbContext>(); //获取注入容器并且获取注入的DbConext
                if (dbContext.Database.EnsureCreated() || !dbContext.Set<FrameworkRole>().Any())
                {   //添加默认数据
                    FrameworkUser user = new FrameworkUser
                    {

                        Account = "Admin",
                        PassWord = "123456",
                        IsDeleted = false
                    };
                    dbContext.FrameworkUsers.Add(user);
                    dbContext.SaveChanges();
                    FrameworkRole Role = new FrameworkRole
                    {
                        Name = "Admin",
                        IsDeleted = false
                    };
                    dbContext.FrameworkRoles.Add(Role);
                    dbContext.SaveChanges();
                    FrameworkUserRole userRole = new FrameworkUserRole
                    {

                        RoleId = Role.Id,
                        UserId = user.Id,
                        IsDeleted = false
                    };
                    dbContext.FrameworkUserRoles.Add(userRole);
                    dbContext.SaveChanges();

                    List<FrameworkMenu> mens = new List<FrameworkMenu>();
                    mens.Add(new FrameworkMenu()
                    {
                        PageName = "系统管理",
                        ActionName = "",
                        Action = "",
                        ControllerName = "目录",
                        Controller = "",
                        ParentId = 0,
                        ClassName = "",
                        LinkUrl = "/",
                        IsMenu = true,
                        IsDir = true,

                        IsDeleted = false
                    });
                    mens.Add(new FrameworkMenu()
                    {
                        PageName = "日志管理",
                        ActionName = "",
                        Action = "",
                        ControllerName = "目录",
                        Controller = "",
                        ParentId = 0,
                        ClassName = "",
                        LinkUrl = "/",
                        IsMenu = true,
                        IsDir = true,

                        IsDeleted = false
                    });


                    mens.Add(new FrameworkMenu()
                    {
                        PageName = "菜单管理",
                        ActionName = "菜单管理",
                        Action = "GetMens",
                        ControllerName = "菜单管理",
                        Controller = "FrameworkMenuApi",
                        ParentId = 1,
                        ClassName = "mycoretest.Api.FrameworkMenuApiController",
                        LinkUrl = "api/FrameworkMenuApi/GetMens",
                        IsMenu = true,
                        IsDir = false,
                        IsDeleted = false
                    });

                    mens.Add(new FrameworkMenu()
                    {
                        PageName = "新建菜单",
                        ActionName = "新建菜单",
                        Action = "CreateMenus",
                        ControllerName = "系统菜单管理",
                        Controller = "FrameworkMenuApi",
                        ParentId = 1,
                        ClassName = "mycoretest.Api.FrameworkMenuApiController",
                        LinkUrl = "api/FrameworkMenuApi/CreateMenus/{PageName}/{IsMen}/{IsDir}/{ParentId}",
                        IsMenu = false,
                        IsDir = false,
                        IsDeleted = false
                    });

                    mens.Add(new FrameworkMenu()
                    {
                        PageName = "日志管理-1-1",
                        ActionName = "日志管理-1",
                        Action = "CreateMenus",
                        ControllerName = "日志管理-1",
                        Controller = "FrameworkMenuApi",
                        ParentId = 2,
                        ClassName = "mycoretest.Api.FrameworkMenuApiController",
                        LinkUrl = "api/FrameworkMenuApi/CreateMenus/{PageName}/{IsMen}/{IsDir}/{ParentId}",
                        IsMenu = false,
                        IsDir = false,
                        IsDeleted = false
                    });

                    mens.Add(new FrameworkMenu()
                    {
                        PageName = "广告其他",
                        ActionName = "广告其他",
                        Action = "CreateMenus",
                        ControllerName = "广告其他",
                        Controller = "FrameworkMenuApi",
                        ParentId = 0,
                        ClassName = "mycoretest.Api.FrameworkMenuApiController",
                        LinkUrl = "api/FrameworkMenuApi/CreateMenus/{PageName}/{IsMen}/{IsDir}/{ParentId}",
                        IsMenu = true,
                        IsDir = false,
                        IsDeleted = false
                    });
                    //7
                    mens.Add(new FrameworkMenu()
                    {
                        PageName = "目录test",
                        ActionName = "目录test",
                        Action = "",
                        ControllerName = "",
                        Controller = "",
                        ParentId = 0,
                        ClassName = "",
                        LinkUrl = "/",
                        IsMenu = true,
                        IsDir = true,
                        IsDeleted = false
                    });
                    //8
                    mens.Add(new FrameworkMenu()
                    {
                        PageName = "编辑提交菜单",
                        ActionName = "编辑提交菜单",
                        Action = "EditSubmitMen",
                        ControllerName = "编辑提交菜单",
                        Controller = "FrameworkMenuApi",
                        ParentId = 7,
                        ClassName = "mycoretest.Api.FrameworkMenuApiController",
                        LinkUrl = "api/FrameworkMenuApi/EditSubmitMen/{MenId}/{ModuleName}",
                        IsMenu = true,
                        IsDir = false,
                    });
                    dbContext.FrameworkMenus.AddRange(mens);


                    dbContext.SaveChanges();

                   
                    List<FrameworkRoleMenuPermission> frameworkRoleMenu = new List<FrameworkRoleMenuPermission>();
                    for (int i = 1; i <= 8; i++)
                    {
                        frameworkRoleMenu.Add(new FrameworkRoleMenuPermission
                        {
                            RoleId = userRole.Id,
                            MenuItemId = i,
                            IsDeleted = false
                        });
                        
                    }
                    dbContext.FrameworkRoleMenuPermissions.AddRange(frameworkRoleMenu);
                    dbContext.SaveChanges();




                }
            }
        }
    }
}
